package ming;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.*;

public class LoginServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String name = request.getParameter("name");
        String pw = request.getParameter("password");
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Connection connection1 = null;
        PreparedStatement preparedStatement1 = null;
        ResultSet resultSet1 = null;
        //3.连接数据库
        try {
            //3.1注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //3.2获取连接
            String url = "jdbc:mysql://localhost:3306/mysql?serverTimezone=UTC";
            connection = DriverManager.getConnection(url,"root","root");
            //3.3写sql
            String sql = "select * from user_login where name = ? and password = ?";
            //3.4获取传输器
            preparedStatement = connection.prepareStatement(sql);
            //3.5设置值
            preparedStatement.setObject(1,name);
            preparedStatement.setObject(2,pw);
            //3.6返回结果集
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                //重定位，如果结果返回true，"跳转"到success.html
                HttpSession session = request.getSession();
                //登录成功
                //将userName存储在session中
                session.setAttribute("userName" , name);
                request.getRequestDispatcher("/success.jsp").forward(request,response);
            }else{

                request.getRequestDispatcher("/unsuccess.jsp").forward(request,response);
            }

        } catch (Exception e) {
            e.printStackTrace();
            //3.6关闭资源
        }finally {{
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }



    }}}
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        doGet(request,response);
//        request.setCharacterEncoding("UTF-8");
//        response.setContentType("text/html;charset=utf-8");
//
//        String name = request.getParameter("name");
//        String pw = request.getParameter("password");
//
//        if(pw.equals("123"))
//        {
//            System.out.println("登录成功");
//            request.setAttribute("result", "登录成功，欢迎"+name);
//        }
//        else
//        {
//            System.out.println("登录失败");
//            request.setAttribute("result", "登录失败，密码"+pw);
//        }
//        request.getRequestDispatcher("/login.jsp").forward(request,response);
    }
}
